package queue;

class MyCircularQueue {

    public int[] elem;
    //public int usedSize;

    public int front;//表示队头
    public int rear;//表示队尾

    public MyCircularQueue(int k) {
        elem = new int[k + 1];
    }

    //入队列,数组尾入
    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    //出队列，数组头出
    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    //从队首获取元素
    public int Front() {
        if (isEmpty()) {
            return -1;//或抛出自定义异常
        }
        return elem[front];
    }

    //获取队尾元素
    public int Rear() {
        if (isEmpty()) {
            return -1;//或抛出自定义异常
        }
        int index = (rear == 0) ? elem.length - 1 : rear - 1;
        return elem[index];
    }

    //检查循环队列是否为空
    public boolean isEmpty() {
        return front == rear;
    }

    //检查循环队列是否已满
    public boolean isFull() {
        return (rear + 1) % elem.length == front;
    }
}